// pagesEnterprise/pages/staffAdd/staffAdd.js
import ajax from "../../../utils/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailId: null,
    formData: {
      gardenId: null,
      name: null,
      phone: null,
      position: null,
      roleId: null,
      wxUserId: null
    },
    roleList: []
  },

  async onSubmit() {
    const { formData, roleList } = this.data;
    const { name, phone, position } = formData
    const regExp = ajax.regExp
    if (!name) return ajax.showToast('请输入真实姓名')
    if (!regExp.chinese.test(name)) return ajax.showToast('请输入正确的姓名')
    if (!position) return ajax.showToast('请输入职位')
    if (!phone) return ajax.showToast('请输入选择用户手机号码')
    const roles = roleList.filter(item => item.select)?.map(item=> item.roleId) || [];
    if (!roles || roles.length < 1) return ajax.showToast('请输入选择权限')
    formData.roleId = roles.join(',')
    const url = formData.id ? '/wx/garden/staff/update' : '/wx/garden/staff/add'
    const res = await ajax.post(url, formData)
    this.setData({ formData: res.data })
    await ajax.showSuccess('操作成功!');
    wx.navigateBack()
  },
  /**
   * input 变化事件
   */
  formItemChange(event) {
    const { formData } = this.data
    const key = event.currentTarget.dataset.key
    const value = event.detail.value || event.detail
    formData[key] = value
    this.setData({ formData })
  },
  /**
   * 手机号选择跳转事件
   */
  phoneEvent() {
    wx.navigateTo({
      url: '/pagesMine/pages/userList/userList',
      events: {
        backEvent: (event) => {
          const { formData } = this.data
          formData.phone = event.wxPhone || ''
          formData.wxUserId = event.id
          this.setData({ formData })
        }
      }
    })
  },
  /**
   * 权限item点击事件
   */
  roleItemEvent(event) {
    const { roleList } = this.data
    const index = event.currentTarget.dataset.index
    roleList[index].select = roleList[index].select ? false : true
    this.setData({ roleList })
  },
  /**
   * 获取员工详情
   */
  async getDetaiRequest() {
    const {roleList} = this.data
    const res = await ajax.get(`/wx/garden/staff/info?id=${this.data.detailId}`)
    roleList.forEach(it => it.select = res.data.role.some(item => item.roleId == it.roleId))
    this.setData({ formData: res.data, roleList })
  },
  /**
   * 查询权限列表
   */
  async getRoleList() {
    const res = await ajax.get('/wx/role/roleList')
    const arr = res.rows.map(item => item.roleKey)
    console.log(arr)
    this.setData({
      roleList: res.rows || []
    })
  },
  async infoRequest () {
    await this.getRoleList()
    if (this.data.detailId) await this.getDetaiRequest();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({ title: options.detailId ? '员工信息' : '新增员工' })
    this.data.formData.gardenId = options.gardenId || null
    this.setData({detailId: options.detailId || null})
    this.infoRequest()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})